package ar.com.freesales.entity;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import ar.com.freesales.enums.RoomTypeEnum;


@Entity
@DiscriminatorValue("BOOKING")
public class Booking extends Ota{
	
	
	@Override
	public String toString() {
		return "soy booking";
	}

	@Override
	public void login(String user, String pass) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        
        httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
                boolean isRedirect=false;
                try {
                    isRedirect = super.isRedirected(request, response, context);
                } catch (ProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!isRedirect) {
                    int responseCode = response.getStatusLine().getStatusCode();
                    if (responseCode == 301 || responseCode == 302) {
                        return true;
                    }
                }
                return isRedirect;
            }
        });
        try {
            HttpGet httpget = new HttpGet(getUrlMain());

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            String pagina = EntityUtils.toString(entity);
            
            
            //FIXME usar select de JQuery
            Document document = Jsoup.parse(pagina);
            Elements hiddens = document.getElementsByTag("input");
            String ses = hiddens.first().val();
            
            System.out.println("session id: " + ses);
            

            
            String urlLog = MessageFormat.format(getUrlLog(), ses);
            System.out.println("url log: " + urlLog);
            
            
            HttpPost httpost = new HttpPost(urlLog);

            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("loginname", user));
            nvps.add(new BasicNameValuePair("password", pass));
            nvps.add(new BasicNameValuePair("ses", ses));
            nvps.add(new BasicNameValuePair("login", "Login"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            System.out.println("Login form get: " + response.getStatusLine());
            
        }catch (Exception e) {
        	//FIXME
			e.printStackTrace();
		}
        
        finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
		
	}

	@Override
	public void changeMassiveRate(Date from, Date to, Float rateValue,
			RoomTypeEnum roomTypeEnum) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void changeMassiveStock(Date from, Date to, Integer stock,
			RoomTypeEnum roomTypeEnum) {
		// TODO Auto-generated method stub
		
	}

}
